<?php
class TasksModel extends BaseModel implements Model {
    function __construct() {
        parent::__construct("tasks", "task_id");
        $this->addField("task_id", Model::TYPE_INT, Model::MOD_PRIMARY);
        $this->addField("task_type", Model::TYPE_VARCHAR);
        $this->addField("task_date", Model::TYPE_DATE);
        $this->addField("parameters", Model::TYPE_JSON);
        $this->addfield("name", Model::TYPE_VARCHAR);
    }

    public function renderAlias($record) {
        $result = $record;
        return $result;
    }

    public function generateName($group, $count) {
        $groupnames = array(
            "Régi top 600",
            "Új cégek",
            "Szakmai szervezetek",
            "GYOSZ-ok",
            "MKIK és vegyes kamarák",
            "Tiszteletbeli konzulok",
            "TPA",
            "US és UK kamarák",
            "Ipari parkok",
            "Vidéki polgármesterek",
            "Magyar szervezetek",
            "Nagykövetségek",
        );
        return $groupnames[$group-1];
    }

    public function getEmailItemFor($taskId) {
        $query = "SELECT * FROM task_email WHERE task_id=$taskId";
        $res = Database::executeQuery($query);
        $data = Database::getDataFromResource($res);
        return $data[0];
    }

    public function updateemailsInTask($taskId, $group, $count, $subject, $content) {
        $query = QueryHelper::update("task_email", array(
            subject => $subject,
            content => $content,
        ), "task_id=$taskId");
        Database::executeQuery($query);
    }

    public function addEmailsToTask($taskId, $group, $count, $subject, $content) {
        $query = QueryHelper::insertInto("task_email", array(
            subject => $subject,
            content => $content,
            count => $count,
            group_id => $group,
            task_id => $taskId,
        ));
        Database::executeQuery($query);
    }

    public function getEmailTasks($page, $rowcount) {
        $start = ($page-1) * $rowcount;
        $limit = " LIMIT $start, $rowcount";
        $query = "SELECT task_id, name FROM tasks ".
//                 "JOIN task_email ON task_email.task_id=tasks.task_id ".
                 "WHERE task_type='email' ".
                 $limit
        ;
        $res = Database::executeQuery($query);
        return Database::getDataFromResource($res);
    }

    public function getEmailTaskCount() {
        $query = "SELECT count(*) AS c FROM tasks WHERE task_type='email'";
        $res = Database::executeQuery($query);
        return Database::getSimpleField($res);
    }

    public function getTodayEmailTasks() {
        $today = date("Y-m-d");
        $query = "SELECT * FROM tasks WHERE task_type = 'email' AND task_date<='$today'";
        $res = Database::executeQuery($query);
        $data = Database::getDatafromResource($res);
        return Database::getModel()->applyTypesForResultset($data);
    }

    public function addEmailTask($to, $subject, $content, $dueDate) {
        $query = QueryHelper::insertInto("tasks", array(
            task_type => "email",
            task_date => $dueDate,
            parameters => json_encode(array(
                to => $to,
                subject => $subject,
                content => str_replace("\n", "", $content),
            ))
        ));
        Database::executeQuery($query);
    }

    public function getemailDataFor($taskId) {
        $emailFields = array();
        $tblName = "";
        $query = "SELECT task_date FROM tasks WHERE task_id=$taskId";
        $res = Database::executeQuery($query);
        $date = Database::getSimpleField($res);
        $query = "SELECT group_id, count, subject, content FROM task_email WHERE task_id=$taskId";
        $res = Database::executeQuery($query);
        $data = Database::getDataFromResource($res);
        $subject = $data[0]['subject'];
        $content = $data[0]['content'];
        $gid = $data[0]['group_id'];
        $count = $data[0]['count'];
        $next = (getDateDifference(date("Y-m-d"), $date)) * $count;
        switch ($gid) {
            case "1" :
            case "2" : {
                $emailFields = array(
                    "email1", "email2",
                );
                $tblName = "extracompanies";
                break;
            }
            case "3" : {
                $emailFields = array(
                    "email",
                    "presidentemail",
                    "secretaryemail",
                );
                $tblName = "lst_szakmaiszervezetek";
                break;
            }
            case "4" : {
                $emailFields = array(
                    "email",
                    "secretaryemail",
                );
                $tblName = "lst_gyosz";
                break;
            }
            case "5" : {
                $emailFields = array(
                    "email",
                    "presidentemail",
                    "secretaryemail",
                );
                $tblName = "lst_mkik";
                break;
            }
            case "6" : {
                $emailFields = array(
                    "email",
                );
                $tblName = "lst_tbkonzulok";
                break;
            }
            case "7" : {
                $emailFields = array(
                    "email1", "email2",
                );
                $tblName = "lst_tpa";
                break;
            }
            case "8" : {
                $emailFields = array(
                    
                );
                break;
            }
            case "9" : {
                $emailFields = array(
                    "email",
                );
                $tblName = "lst_iparipark";
                break;
            }
            case "10" : {
                $emailFields = array(
                    "email",
                    "contactemail",
                );
                $tblName = "lst_pm";
                break;
            }
            case "11" : {
                $emailFields = array(
                    "email1",
                    "email2",
                );
                $tblName = "lst_organizations";
                break;
            }
            case "12" : {
                $emailFields = array(
                    "email1",
                    "email2",
                );
                $tblName = "lst_embassies";
                break;
            }
        }
        $query = "SELECT " . implode(",", $emailFields) . " FROM $tblName LIMIT $next, $count";
        $res = Database::executeQuery($query);
        $result = Database::getDatafromResource($res);
        for ($i = 0; $i < count($result); $i++) {
            $result[$i]['subject'] = $subject;
            $result[$i]['content'] = $content;
            $result[$i]['emailfields'] = $emailFields;
        }
        return $result;
    }

    public function isAuthorized($operation, $subject = "") {
        return true;
//        if (Auth::isAdmin()) { return true; }
/*
        switch ($operation) {
            case "update" :
            case "delete" :
            case "addnew" : {
                if (Auth::canChangeDrivers()) { return true; }
            }
            case "list" : {
                if (Auth::canListDrivers()) { return true; }
            }
        }
        return false;
*/
    }

    public function modifyDatabaseInput($data) {
        return $data;
    }
}

?>
